package com.zw.yzk.kabu.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.model.AlbumModel;
import com.zw.yzk.kabu.utils.PhoneUtils;
import com.zw.yzk.kabu.utils.QNImageUtils;
import com.zw.yzk.kabu.view.activity.CosplayDetailsActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhanwei on 2017/7/5.
 */

public class AlbumAdapter extends RecyclerView.Adapter<AlbumAdapter.Holder> {

    private List<AlbumModel> dataList;
    private List<AlbumModel> realList;
    private Context context;

    public AlbumAdapter(List<AlbumModel> list) {
        dataList = new ArrayList<>();
        if (list != null) {
            dataList.addAll(list);
        }
        dealDataList();
    }

    public void refresh(List<AlbumModel> list) {
        dataList.clear();
        if (list != null) {
            dataList.addAll(list);
        }
        dealDataList();
        notifyDataSetChanged();
    }

    public void add(List<AlbumModel> list) {
        if (list != null) {
            dataList.addAll(list);
            dealDataList();
            notifyDataSetChanged();
        }
    }

    public int getLastItemId() {
        if (dataList.isEmpty()) {
            return 0;
        }
        return dataList.get(dataList.size() - 1).getImageId();
    }

    private void dealDataList() {
        if (realList == null) {
            realList = new ArrayList<>();
        } else {
            realList.clear();
        }
        int size = dataList.size();
        for (int i = 0; i < size; i++) {
            realList.add(dataList.get(i));
            if (i != size - 1 && dataList.get(i).getType() != dataList.get(i + 1).getType()) {
                switch (realList.size() % 3) {
                    case 0:
                        break;
                    case 1:
                        AlbumModel add1 = new AlbumModel();
                        add1.setType(dataList.get(i).getType());
                        realList.add(add1);
                    case 2:
                        AlbumModel add2 = new AlbumModel();
                        add2.setType(dataList.get(i).getType());
                        realList.add(add2);
                        break;
                    default:
                        break;
                }
            }
        }
        int realSize = realList.size();
        for (int i = 0; i < realSize; i++) {
            if (i == 0 || realList.get(i).getType() != realList.get(i - 1).getType()) {
                realList.get(i).setDateVisiable(View.VISIBLE);
                if(i + 1 < realSize) {
                    realList.get(i + 1).setDateVisiable(View.INVISIBLE);
                }
                if(i + 2 < realSize) {
                    realList.get(i + 2).setDateVisiable(View.INVISIBLE);
                }
            }

        }
    }

    @Override
    public AlbumAdapter.Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null) {
            context = parent.getContext();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.recycler_item_album_list, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(AlbumAdapter.Holder holder, int position) {
        final AlbumModel model = realList.get(position);
        holder.date.setText(model.getDate());
        holder.date.setVisibility(model.getDateVisiable());

        int imageWidth = (int) (((PhoneUtils.getScreenWidth(context) - 32 * context.getResources().getDisplayMetrics().density)) / 3);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.image.getLayoutParams();
        if (params == null) {
            params = new LinearLayout.LayoutParams(imageWidth, imageWidth);
        } else {
            params.height = imageWidth;
            params.width = imageWidth;
        }
        holder.image.setLayoutParams(params);
        Glide.with(context)
                .load(QNImageUtils.getThumbnail(model.getImageUrl(), imageWidth, imageWidth))
                .centerCrop()
                .into(holder.image);

        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, CosplayDetailsActivity.class);
                intent.putExtra(ActivityConstant.OPEN_COS_DETAIL_ID, model.getCosId());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return realList.size();
    }

    public static class Holder extends RecyclerView.ViewHolder {
        @Bind(R.id.date)
        public TextView date;
        @Bind(R.id.image)
        public ImageView image;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

    }
}
